package org.zecontech.extraplus.init;

import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.zecontech.extraplus.ExtraPlus;
import org.zecontech.extraplus.entity.Kelong;
import org.zecontech.extraplus.entity.Nailong;

import java.util.Locale;

// 模组实体
public class ModEntities {

    // 实体总线
    public static final DeferredRegister<EntityType<?>> ENTITIES =
            DeferredRegister.create(Registries.ENTITY_TYPE, ExtraPlus.MOD_ID);

    public static final DeferredHolder<EntityType<? extends Entity>, EntityType<Kelong>> KELONG =
            register("kelong", EntityType.Builder.of(Kelong::new, MobCategory.CREATURE)
                    .sized(0.75F, 0.42F)
                    .eyeHeight(0.2751F)
                    .clientTrackingRange(10));
    public static final DeferredHolder<EntityType<? extends Entity>, EntityType<Nailong>> NAILONG =
            register("nailong", EntityType.Builder.of(Nailong::new, MobCategory.CREATURE)
                    .sized(0.75F, 0.42F)
                    .eyeHeight(0.2751F)
                    .clientTrackingRange(10));

    /**
     * 注册实体
     * @param name 名字
     * @param builder 构造器
     * @return 实体
     * @param <E> 实体
     */
    public static <E extends Entity> DeferredHolder<EntityType<? extends Entity>, EntityType<E>> register(String name, EntityType.Builder<E> builder) {
        return register(name, builder, true);
    }

    /**
     * 注册实体
     * @param name 名字
     * @param builder 构造器
     * @param serialize 是否扫描
     * @return 实体
     * @param <E> 实体
     */
    public static <E extends Entity> DeferredHolder<EntityType<? extends Entity>, EntityType<E>> register(String name, EntityType.Builder<E> builder, boolean serialize) {
        final String id = name.toLowerCase(Locale.ROOT);
        return ENTITIES.register(id, () -> {
            if (!serialize) builder.noSave();
            return builder.build(ResourceKey.create(Registries.ENTITY_TYPE, ResourceLocation.fromNamespaceAndPath( ExtraPlus.MOD_ID, id)));
        });
    }
}